package com.pagepub.expo.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.stereotype.Repository;

import com.pagepub.expo.entity.AppRole;

@Repository
public class AppRoleDao  {
	@Autowired
	@Qualifier("jdbcTemplate")
	private JdbcTemplate template;
	
	@Autowired
	@Qualifier("appSeqDao")
	private AppSeqDao appSeqDao;
	
	private static String tableName="App_ROLE";
	
	private static String QUERY_ALL = "SELECT roleId,roleName,roleDesc,status,rights FROM "+tableName;

	private static String QUERY_ALL_COUNT = "SELECT COUNT(*) FROM "+  tableName;
	
	private static String INSERT_SQL = "INSERT  INTO "+tableName+" (" +
			"roleId,roleName,roleDesc,status,rights)VALUES(" +
			":roleId,:roleName,:roleDesc,:status,:rights)";

	private static String DELETE_SQL = "DELETE FROM "+ tableName +" WHERE roleId=?";

	private static String UPDATE_SQL = "UPDATE "+ tableName + " SET " +
			"roleName=:roleName,roleName=:roleName,status=:status,rights=:rights"+
			" WHERE roleId=:roleId";

	private final ParameterizedRowMapper<AppRole> mapper = new ParameterizedRowMapper<AppRole>() {
		public AppRole mapRow(ResultSet rs, int rowNum) throws SQLException {
			AppRole obj = new AppRole();
			obj.setRoleId(rs.getLong("roleId"));
			obj.setRoleName(rs.getString("roleName"));
			obj.setRoleDesc(rs.getString("roleDesc"));
			obj.setStatus(rs.getShort("status"));
			obj.setRights(rs.getString("rights"));
			return obj;
		}
	};

	public List<AppRole> findAll() {
		return template.query(QUERY_ALL, mapper);
	}

	public AppRole findById(Long id) {
		List<AppRole> list = template.query(QUERY_ALL + " WHERE roleId=?",
				new Object[] { id }, mapper);
		if (list.size() == 0) {
			return null;
		} else
			return list.get(0);
	}
	
	
	public List<AppRole> findByUserId(Long id) {
		String sql="select t1.roleId,roleName,roleDesc,status,rights from App_ROLE t1 , APP_USER_ROLE t2 where t1.roleid = t2.roleid and t2.userid=?";
		
		return  template.query(sql,
				new Object[] { id }, mapper);
	
	}
	
	

	
	public void insert(AppRole bean) {
		if(bean.getRoleId()==null){
			bean.setRoleId(new Long(appSeqDao.getSequence("app_role_seq")));
		}		
		SqlParameterSource ps = new BeanPropertySqlParameterSource(bean);
		NamedParameterJdbcTemplate npTemplate = new NamedParameterJdbcTemplate(
				template.getDataSource());
		npTemplate.update(INSERT_SQL, ps);

	}

	public void delete(Long id) {
		template.update(DELETE_SQL, id);
	}

	public void update(AppRole bean) {
		SqlParameterSource ps = new BeanPropertySqlParameterSource(bean);
		NamedParameterJdbcTemplate npTemplate = new NamedParameterJdbcTemplate(
				template.getDataSource());
		npTemplate.update(UPDATE_SQL, ps);
	}
	
	public int getCount(Map<String, String> param){
		int count = template.queryForInt(QUERY_ALL_COUNT);
		return count;
	}
	
	public List findByPage(int currentPage,int pageSize,Map<String, String> param){
		List lists = template.query(QUERY_ALL + " limit ?,?",
				new Object[] { currentPage, pageSize}, mapper);
		return lists;
		
	}
}
